package com.lpf.business.service;

import com.lpf.common.response.Result;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Component
@FeignClient(name = "flowable-system", url = "http://localhost:8082/flowable/api")
public interface FeignClientService {

    /**
     * 流程部署
     *
     * @param modelId 流程ID，来自 ACT_DE_MODEL
     */
    @GetMapping(value = "/deploy/{modelId}")
    Result<Deployment> deploy(@PathVariable(value = "modelId") String modelId);

    /**
     * 启动流程
     *
     * @param deployId    部署的流程 Id，来自 ACT_RE_PROCDEF
     * @param userId      用户 Id（流程的的发起人）
     * @param businessKey 数据 Key（业务键），一般为表单数据的 ID，仅作为表单数据与流程实例关联的依据
     */
    @GetMapping(value = "/start/{deployId}/{userId}/{businessKey}")
    Result<Map<String, String>> start(
            @PathVariable(value = "deployId") String deployId,
            @PathVariable(value = "userId") String userId,
            @PathVariable(value = "businessKey") String businessKey);

    /**
     * 获取当前候选组
     *
     * @param taskId 任务 Id，来自 ACT_RU_TASK
     */
    @GetMapping(value = "/taskInfo/{taskId}")
    Result<List<String>> taskInfo(@PathVariable(value = "taskId") String taskId);

    /**
     * 设置任务参数
     *
     * @param taskId 任务ID
     * @param map    键值对
     */
    @PostMapping(value = "/setVariables")
    Result<String> setVariables(
            @RequestParam(value = "taskId") String taskId,
            @RequestBody Map<String, Object> map);

    /**
     * 设置任务参数
     *
     * @param taskId 任务ID
     * @param key    键
     * @param value  值
     */
    @PostMapping(value = "/setVariable")
    Result<String> setVariable(
            @RequestParam(value = "taskId") String taskId,
            @RequestParam(value = "key") String key,
            @RequestParam(value = "value") Object value);

    /**
     * 设置任务参数
     *
     * @param taskId 任务ID
     * @param key    键
     * @param value  值
     */
    @PostMapping(value = "/setListVariable")
    Result<String> setListVariable(
            @RequestParam(value = "taskId") String taskId,
            @RequestParam(value = "key") String key,
            @RequestParam(value = "value") List<String> value);

    /**
     * 任务处理
     *
     * @param taskId 任务 Id，来自 ACT_RU_TASK
     */
    @GetMapping(value = "/task/{taskId}")
    Result<String> task(@PathVariable(value = "taskId") String taskId);

    /**
     * 任务处理
     *
     * @param taskId   任务 Id，来自 ACT_RU_TASK
     * @param assignee 设置审核人
     * @param map      完成任务需要的条件参数
     */
    @PostMapping(value = "/task")
    Result<String> taskByAssignee(
            @RequestParam(value = "taskId") String taskId,
            @RequestParam(value = "assignee") String assignee,
            @RequestBody Map<String, Object> map);

    /**
     * 中止流程
     *
     * @param processId 流程ID
     */
    @GetMapping(value = "/deleteProcess/{processId}")
    Result<String> deleteProcess(@PathVariable(value = "processId") String processId);

    /**
     * 获取正在运行的 business_key 列表
     */
    @GetMapping(value = "/getRuntimeBusinessKey")
    Result<List<String>> getRuntimeBusinessKey();

    /**
     * 根据用户，获取需要审核的业务键 business_key 列表
     *
     * @param userId 用户 Id
     */
    @GetMapping(value = "/getRuntimeBusinessKeyByUser/{userId}")
    Result<List<Map<String, Object>>> getRuntimeBusinessKeyByUserId(@PathVariable(value = "userId") String userId);

    /**
     * 根据组，获取需要审核的业务键 business_key 列表
     *
     * @param groupIds 组 Id
     */
    @PostMapping(value = "/getRuntimeBusinessKeyByGroup")
    Result<List<Map<String, Object>>> getRuntimeBusinessKeyByGroup(@RequestBody List<String> groupIds);

    /**
     * 获取用户审核历史
     *
     * @param userId 发起人 Id
     */
    @GetMapping(value = "/getHistoryByUser/{userId}")
    Result<List<Map<String, Object>>> getHistoryByUser(@PathVariable(value = "userId") String userId);

    /**
     * 通过流程实例 Id，判断流程是否结束
     *
     * @param processInstanceId 流程实例 Id
     * @return true 结束，false 未结束
     */
    @GetMapping(value = "/checkProcessInstanceFinish/{processInstanceId}")
    Result<Boolean> checkProcessInstanceFinish(@PathVariable(value = "processInstanceId") String processInstanceId);


    /**
     * 根据任务节点获取流程实例 Id
     *
     * @param taskId 任务节点 Id
     */
    @GetMapping(value = "/getProcessInstanceIdByTaskId/{taskId}")
    Result<String> getProcessInstanceIdByTaskId(@PathVariable(value = "taskId") String taskId);

    /**
     * 根据流程实例 ID 获取任务进度流程图
     *
     * @param processInstanceId 流程实例 Id
     */
    @RequestMapping(value = "/getProcessDiagram/{processInstanceId}", method = RequestMethod.GET)
    void getProcessDiagram(@PathVariable(value = "processInstanceId") String processInstanceId, HttpServletResponse httpServletResponse);

    /**
     * 根据任务 ID 获取任务进度流程图
     *
     * @param taskId 任务节点 Id
     */
    @GetMapping(value = "/getTaskProcessDiagram/{taskId}")
    void getTaskProcessDiagram(@PathVariable(value = "taskId") String taskId, HttpServletResponse httpServletResponse);

}
